package com.px.lovepath;

import java.util.LinkedList;

import android.app.Application;
import android.os.Looper;

import com.px.lovepath.datebase.DataProvider;
import com.px.lovepath.datebase.DatabaseManager;
import com.px.lovepath.domain.config.ConfigInfo;
import com.px.lovepath.ui.general.LoveActivity;
import com.px.lovepath.ui.home.MenuAnimations;
import com.px.lovepath.utils.network.HttpClientManager;
import com.px.lovepath.utils.network.NetworkMonitor;

public class LovePath extends Application {
	
	private static LovePath mSingleton;
	private LoveActivity mCurrentActivity = null;
	private LinkedList<LoveAppListener> mListenerList = new LinkedList<LoveAppListener>();
	
	// ### 构造函数 ###
	public LovePath() {
		mSingleton = this;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		LovePathEnv.startUp(this);
		DatabaseManager.startup(this);
		NetworkMonitor.startup(this);
		HttpClientManager.startup(this, NetworkMonitor.get());
		DataProvider.startup(this, LovePathEnv.get(), DatabaseManager.get());
		ConfigInfo.startUp(this);
		MenuAnimations.initOffset(this);
	}


	// ### 属性 ###
	public LoveActivity getCurrentActivity() {
		assert checkAccess();
		
		return mCurrentActivity;
	}

	// ### 方法 ###
	public void addAppListener(LoveAppListener appListener) {
		assert checkAccess();
		assert appListener != null;

		mListenerList.add(appListener);
	}
	public static LovePath get() {
		return mSingleton;
	}

	// ### 隐藏方法 ###
	protected void onActivityPause(LoveActivity activity) {
		assert activity != null;

		for (LoveAppListener listener : mListenerList) {
			listener.onActivityPause(activity);
		}
	}
	protected void onActivityResume(LoveActivity activity) {
		assert activity != null;
		
		for (LoveAppListener listener : mListenerList) {
			listener.onActivityResume(activity);
		}
		
		mCurrentActivity = activity;
	}
	
	// ### 实现方法 ###
	private boolean checkAccess() {
		return Thread.currentThread() == Looper.getMainLooper().getThread(); 
	}
}
